package cn.tedu._05article.base.interceptors;

import cn.tedu._05article.pojo.vo.UserVO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import java.io.IOException;

@Slf4j
public class LoginCheckinterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        log.debug("进入登录状态效验的拦截器....");
        HttpSession session = request.getSession(); //获取会话session
        UserVO userVO = (UserVO) session.getAttribute("user");//从会话中获取用户标识
        if (userVO == null){
            response.sendRedirect("/login.html");
            log.debug("用户未登录，跳转到登录页面");
            return false;
        }
        return true;
    }
}
